package com.hxiaoyi.cloud.controller;

import com.hxiaoyi.cloud.entities.CommonResult;
import com.hxiaoyi.cloud.entities.Payment;
import com.hxiaoyi.cloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * 消费 OrderController
 * @author 黄敬意
 * @date 2022-02-02 22:09
 **/
@RestController
@Slf4j
public class OrderController {

    /**单机版的服务*/
    //public static final String PAYMENT_URL = "http://127.0.0.1:8001";
    /**
     * eureka的服务，只需要写eureka上注册的微服务名字，写了这个就会有多台服务，这个时候就要开启负载均衡，在config里
     * 加上@LoadBalanced注解开启RestTemplate 的负载均衡
     * */
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private LoadBalancer loadBalancer;
    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 调用8001的服务端口create
     * @param payment
     * @return CommonResult
     */
    @PostMapping(value = "/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        log.info("------开始调用8001的服务端口create------");
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create", payment, CommonResult.class);
    }

    /**
     * 调用8001查询端口getPayment
     * @param id
     * @return CommonResult
     */
    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        log.info("------开始调用8001查询端口getPayment------");
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping(value = "/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getForEntity(@PathVariable("id") Long id) {
        log.info("------开始调用8001查询端口getForEntity------");
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return forEntity.getBody();
        } else {
            return new CommonResult<>(444, "请求失败请联系管理员" );
        }
    }

    /**获取注册中心服务端口号*/
    @GetMapping(value = "/consumer/payment/lb")
    public String getPaymentLb() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/lb", String.class);
    }

    @GetMapping("consumer/payment/zipkin")
    public String paymentZipkin(){
        String result = restTemplate.getForObject("http://localhost:8001" + "/payment/zipkin/", String.class);
        return result;
    }
}
